package minidb.parse.exps;
import java.io.Serializable;
import java.util.*;
import minidb.parse.*;

public class Condition implements Serializable
{
	public int op;
	public Condition condition1;
	public Condition condition2;
	public Predicate predicate;
	
	public List<Predicate> predicateList;
	
	public Condition(Predicate p)
	{
		this.op=-1;
		this.condition1=null;
		this.condition2=null;
		this.predicate=p;

		this.predicateList=new ArrayList<Predicate>();
		this.predicateList.add(p);
	}
	
	public Condition(int op,Condition c1,Condition c2)
	{
		this.op=op;
		this.condition1=c1;
		this.condition2=c2;
		this.predicate=null;
	
		this.predicateList=new ArrayList<Predicate>();
		this.predicateList.addAll(c1.predicateList);
		/*special condition 'not'*/
		if(c2!=null)
			this.predicateList.addAll(c2.predicateList);
	}
	
	public Condition DupCondition()
	{
		return this._DupCondition(this);
	}
	
	private Condition _DupCondition(Condition c)
	{
		if(c==null)
			return null;
		else if(c.op==-1)
			return new Condition(c.predicate.DupPredicate());
		else
		{
			Condition c1=this._DupCondition(c.condition1);
			Condition c2=this._DupCondition(c.condition2);
			return new Condition(c.op,c1,c2);
		}
	}
	
	public List<Predicate> GetPredicateList()
	{
		return this.predicateList;
	}
	
	public String Print()
	{
		if(this.op==sym.NOT)
			return "NOT("+this.condition1.Print()+")";
		else if(this.op==-1)
			return this.predicate.Print();
		else
			return "("+this.condition1.Print()+")"+SQLExp.GetSym(this.op)+"("+this.condition2.Print()+")";
	}
	
	public String Print_Base()
	{
		if(this.op==sym.NOT)
			return "NOT("+this.condition1.Print_Base()+")";
		else if(this.op==-1)
			return this.predicate.Print_Base();
		else
			return "("+this.condition1.Print_Base()+")"+SQLExp.GetSym(this.op)+"("+this.condition2.Print_Base()+")";
	}
}












